package com.cefolim.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cefolim.bean.EmployeBean;
import com.cefolim.dao.IEmployeDao;
import com.cefolim.model.Departement;
import com.cefolim.model.Employe;
import com.cefolim.model.Job;

//public class EmployeDao extends AbstractDAO<Employe> implements IEmployeDao {

public class EmployeDao extends HibernateDaoSupport implements IEmployeDao,	Serializable {

	private static final long serialVersionUID = 1L;

	@Override
	public List<Employe> getEmployeByCriteria(EmployeBean emp) {

		StringBuffer requete = new StringBuffer("from Employe where 1=1 ");

		if (emp != null) {
			if (!StringUtils.isBlank(emp.getNom())) {
				System.out.println("nom => "+emp.getNom());
				requete.append(" AND nom= :nom ");
			}
			if (!StringUtils.isBlank(emp.getPrenom())) {
				System.out.println("Prenom => "+emp.getPrenom());
				requete.append(" AND prenom= :prenom ");
			}
			if (emp.getSalaire() != 0) {
				System.out.println("Salaire => "+emp.getSalaire());
				requete.append(" AND salaire= :salaire ");
			}
			if (emp.getDep()!=null && emp.getDep()>0) {
				System.out.println("Dept => "+emp.getDep());
				requete.append(" AND departement= :dept ");
			}
			if (emp.getDateStart()!=null) {
				System.out.println("Date Start => "+emp.getDateStart());
				requete.append(" AND dateNaiss>=  :dateStart");
			}
			if (emp.getDateEnd() != null) {
				System.out.println("Date End => " + emp.getDateEnd());
				requete.append(" AND dateNaiss < :dateEnd ");
			}
		}

		Query query = getSession().createQuery(requete.toString());

		if (emp != null) {
			if (!StringUtils.isBlank(emp.getNom())) {
				query.setString("nom", emp.getNom());
			}
			if (!StringUtils.isBlank(emp.getPrenom())) {
				query.setString("prenom", emp.getPrenom());
			}
			if (emp.getSalaire() != 0) {
				query.setFloat("salaire", emp.getSalaire());
			}
			if (emp.getDep()!=null && emp.getDep()>0) {
				query.setLong("dept", emp.getDep());
			}
			if (emp.getDateStart()!=null) {
				 query.setDate("dateStart", emp.getDateStart());
			}
			if (emp.getDateEnd() != null) {
			      query.setDate("dateEnd", emp.getDateEnd());
			}
		}

		List<Employe> list = query.list();

		return list;
	}

	
	@Override
	public List<Departement> getAllDepartement() {
		
		Query query = getSession().createQuery("from Departement");
		
		List<Departement> list = query.list();
		
		return list;
	}

	
	@Override
	public List<Job> getAllJob() {
		
		Query query = getSession().createQuery("from Job");
		
		List<Job> listJob = query.list();
		
		return listJob;
	}
	
	
	@Override
	public void saveOrUpdate(Employe employe) {
		
		getHibernateTemplate().saveOrUpdate(employe);

	}

	
	@Override
	public void remove(Employe employe) {
		
		getHibernateTemplate().delete(employe);

	}

	@Override
	public List<String> getAllEmployesByNames() {
		Query query = getSession().createQuery(" select nom from Employe ");
		List<String> list = query.list();
		return list;
	}


	@Override
	public List<String> getEmployesByNames(String begin) {
		String requet = "select e.nom from Employe e ";
		if(!StringUtils.isBlank(begin))
		{
			requet += "where e.nom like '"+begin+"%'";
		}
		Query query = getSession().createQuery(requet);
		List<String> list = query.list();
		return list;
	}

	// @Override
	// public String getPojoClassName() {
	// return Employe.class.getCanonicalName().toString();
	// }

}